package com.hustmm.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hustmm.domain.MmMaterialProperties;
import com.hustmm.mapper.MmMaterialMapper;
import com.hustmm.domain.MmMaterial;
import com.hustmm.service.IMmMaterialService;

/**
 * 材料主Service业务层处理
 * 
 * @author WUDI
 * @date 2024-05-23
 */
@Service
public class MmMaterialServiceImpl implements IMmMaterialService 
{
    @Autowired
    private MmMaterialMapper mmMaterialMapper;

    /**
     * 查询材料主
     * 
     * @param matId 材料主主键
     * @return 材料主
     */
    @Override
    public MmMaterial selectMmMaterialByMatId(String matId)
    {
        return mmMaterialMapper.selectMmMaterialByMatId(matId);
    }

    /**
     * 查询材料主列表
     * 
     * @param mmMaterial 材料主
     * @return 材料主
     */
    @Override
    public List<MmMaterial> selectMmMaterialList(MmMaterial mmMaterial)
    {
        return mmMaterialMapper.selectMmMaterialList(mmMaterial);
    }

    /**
     * 新增材料主
     * 
     * @param mmMaterial 材料主
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMmMaterial(MmMaterial mmMaterial)
    {
        mmMaterial.setMatId(IdUtils.simpleUUID());
        mmMaterial.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        mmMaterial.setCreateTime(DateUtils.getNowDate());
        int length = mmMaterialMapper.selectMmMaterialList(mmMaterial).size();
        if (length > 0){
//            return 0;
            throw new RuntimeException("材料名重复");
        }
        int rows = mmMaterialMapper.insertMmMaterial(mmMaterial);

        insertMmMaterialProperties(mmMaterial);
        return rows;
    }

    /**
     * 修改材料主
     * 
     * @param mmMaterial 材料主
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMmMaterial(MmMaterial mmMaterial)
    {
        mmMaterial.setUpdateTime(DateUtils.getNowDate());
        mmMaterial.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        mmMaterialMapper.deleteMmMaterialPropertiesByMatId(mmMaterial.getMatId());
        insertMmMaterialProperties(mmMaterial);
        return mmMaterialMapper.updateMmMaterial(mmMaterial);
    }

    /**
     * 批量删除材料主
     * 
     * @param matIds 需要删除的材料主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMmMaterialByMatIds(String[] matIds)
    {
        mmMaterialMapper.deleteMmMaterialPropertiesByMatIds(matIds);
        return mmMaterialMapper.deleteMmMaterialByMatIds(matIds);
    }

    /**
     * 删除材料主信息
     * 
     * @param matId 材料主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMmMaterialByMatId(String matId)
    {
        mmMaterialMapper.deleteMmMaterialPropertiesByMatId(matId);
        return mmMaterialMapper.deleteMmMaterialByMatId(matId);
    }

    /**
     * 新增材料属性信息
     * 
     * @param mmMaterial 材料主对象
     */
    public void insertMmMaterialProperties(MmMaterial mmMaterial)
    {
        List<MmMaterialProperties> mmMaterialPropertiesList = mmMaterial.getMmMaterialPropertiesList();
        String matId = mmMaterial.getMatId();
        if (StringUtils.isNotNull(mmMaterialPropertiesList))
        {
            List<MmMaterialProperties> list = new ArrayList<MmMaterialProperties>();
            for (MmMaterialProperties mmMaterialProperties : mmMaterialPropertiesList)
            {
                mmMaterialProperties.setPropertyId(IdUtils.simpleUUID());
                mmMaterialProperties.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                mmMaterialProperties.setCreateTime(DateUtils.getNowDate());
                mmMaterialProperties.setMatId(matId);
                list.add(mmMaterialProperties);
            }
            if (list.size() > 0)
            {
                mmMaterialMapper.batchMmMaterialProperties(list);
            }
        }
    }
//密码测试
//    public static void main(String[] args) {
//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        String result = encoder.encode("mcqlwzj@hust");
//        System.out.println(result);
//        boolean matches = encoder.matches("mcqlwzj@hust", result);
//        System.out.println(matches);
//
//    }
}
